namespace CSharpPro._6._1;

public class Demo
{
        public static void BubbleSort(int[] arr)
        {
            if (arr == null || arr.Length <= 1)
                return;
            
            int n = arr.Length;
            bool isSwapped;
            
            for (int i = 0; i < n - 1; i++)
            {
                isSwapped = false;
                for (int j = 0; j < n - i - 1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]);
                        isSwapped = true;
                    }
                }
                
                if (!isSwapped)
                    break;
            }
        }
        
        public static void PrintArray(int[] arr)
        {
            if (arr == null)
            {
                Console.WriteLine("数组为空");
                return;
            }
            Console.WriteLine(string.Join(" ", arr));
        }
}